package com.lance.shiro.mapper;

import com.lance.shiro.entity.*;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface SalesNotificationMapper {

    //查询
    @Select("select * from i_salesNotification where 1=1 order by submitNotifySaleTime desc  ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "propertyId", column = "propertyId"),
            @Result(property = "propertyListId", column = "propertyListId"),
            @Result(property = "solicitorId", column = "solicitorId"),
            @Result(property = "solicitor", column = "solicitorId",javaType = ISolicitor.class,
                    one = @One(select = "com.lance.shiro.mapper.SolicitorMapper.fetchById")),
            @Result(property = "customerId", column = "customerId"),
            @Result(property = "customer", column = "customerId",javaType = IUser.class,
                    one = @One(select = "com.lance.shiro.mapper.UserMapper.get")),
            @Result(property = "property", column = "propertyId", javaType = IProperty.class,
                    one = @One(select = "com.lance.shiro.mapper.PropertyMapper.get")),
            @Result(property = "propertyList", column = "propertyListId", javaType = IPropertyList.class,
                    one = @One(select = "com.lance.shiro.mapper.PropertyListMapper.get")),
            @Result(property = "agentId", column = "agentId"),
            @Result(property = "agent", column = "agentId", javaType = IUser.class,
                    one = @One(select = "com.lance.shiro.mapper.UserMapper.get")),
    })
    ArrayList<ISalesNotification> findAll();


    @Select("select * from i_salesNotification where id=#{id} ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "propertyId", column = "propertyId"),
            @Result(property = "propertyListId", column = "propertyListId"),
            @Result(property = "solicitorId", column = "solicitorId"),
            @Result(property = "solicitor", column = "solicitorId",javaType = ISolicitor.class,
                    one = @One(select = "com.lance.shiro.mapper.SolicitorMapper.fetchById")),
            @Result(property = "customerId", column = "customerId"),
            @Result(property = "customer", column = "customerId",javaType = IUser.class,
                    one = @One(select = "com.lance.shiro.mapper.UserMapper.get")),
            @Result(property = "property", column = "propertyId", javaType = IProperty.class,
                    one = @One(select = "com.lance.shiro.mapper.PropertyMapper.get")),
            @Result(property = "propertyList", column = "propertyListId", javaType = IPropertyList.class,
                    one = @One(select = "com.lance.shiro.mapper.PropertyListMapper.get")),
            @Result(property = "agentId", column = "agentId"),
            @Result(property = "agent", column = "agentId", javaType = IUser.class,
                    one = @One(select = "com.lance.shiro.mapper.UserMapper.get")),
    })
    ISalesNotification get(int id);

    @Insert("insert into i_salesNotification (propertyId,propertyListId,agentId,solicitorId,customerId,saleNotificationContent,submitNotifySaleTime) " +
            "values(#{propertyId},#{propertyListId},#{agentId},#{solicitorId},#{customerId},#{saleNotificationContent},now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public int saveAdd(ISalesNotification entity);

    //更新
    @Update("update i_salesNotification set propertyId=#{propertyId},propertyListId=#{propertyListId},agentId=#{agentId},solicitorId=#{solicitorId},customerId=#{customerId},saleNotificationContent=#{saleNotificationContent} " +
            ",submitNotifySaleTime=now() where id=#{id}")
    int saveUpdate(ISalesNotification entity);

    @Update("delete from i_salesNotification where id=#{id} ")
    public void delete(int id);

    @Select("select * from i_salesNotification where 1=1 and propertyId=#{propertyId} ")
    ArrayList<ISalesNotification> findAllByPropertyId(Integer propertyId);

    @Select("select * from i_salesNotification where 1=1 and propertyId= #{propertyId} and agentId=#{agentId} LIMIT 1")
    ISalesNotification findByPropertyIdAgentId(@Param("propertyId") int propertyId, @Param("agentId") int agentId);

    @Select("select * from i_salesNotification where 1=1 and id != #{id} and propertyId= #{propertyId} and agentId=#{agentId} LIMIT 1")
    ISalesNotification findByPropertyIdAgentIdUpdate(@Param("id") int id, @Param("propertyId") int propertyId, @Param("agentId") int agentId);
}
